﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasicForms;
using System.Data.SqlClient;

namespace ReceivingDelivering.ReportGenerators
{
    public partial class FrmTongHopSoLuongNhapXuat : FrmSupportPrintComponents
    {
        public FrmTongHopSoLuongNhapXuat()
        {
            InitializeComponent();
        }

        protected override void LoadDataReport()
        {
            base.LoadDataReport();
            var dataSouce = DB.Products.Select(
                    p => new
                    {
                        p.ProductNo,
                        ProudctName = p.ProductName,
                        QuantityReceiving = DB.ReceivingBillInfos.Where(info => info.ProductID == p.ProductID)
                            .Join(DB.ReceivingBills,
                                  outer => outer.ReceivingID,
                                  inner => inner.ReceivingID,
                                  (inner, outer) => new
                                  {
                                      inner.ReceivingQuantity,
                                      outer.ReceivingCreateDate
                                  }).Where(q => q.ReceivingCreateDate >= dtNgayBatDau.DateTime && q.ReceivingCreateDate <= dtNgayKetThuc.DateTime)
                                  .Sum(q => q.ReceivingQuantity),
                        QuantityDelivering = DB.DeliveringBillInfos.Where(info => info.ProductID == p.ProductID)
                            .Join(DB.DeliveringBills,
                                outer => outer.DeleveringBillID,
                                inner => inner.DeleveringID,
                                (inner, outer) => new
                                {
                                    inner.DeleveringQuantity,
                                    outer.CreateDate
                                }).Where(q => q.CreateDate >= dtNgayBatDau.DateTime && q.CreateDate <= dtNgayKetThuc.DateTime)
                                .Sum(q => q.DeleveringQuantity)
                    }
                ).Where(p => p.QuantityDelivering != null || p.QuantityReceiving != null).AsEnumerable()
                .Select(
                    (p, index) => new
                    {
                        rowindex = index + 1,
                        p.ProductNo,
                        p.ProudctName,
                        p.QuantityDelivering,
                        p.QuantityReceiving
                    }
                );
            gridControl.DataSource = dataSouce;
            #region Định dạng bảng
            int i = 0;
            foreach (DevExpress.XtraGrid.Views.BandedGrid.BandedGridColumn col in bandedGridView1.Columns)
            {
                DevExpress.XtraGrid.Views.BandedGrid.GridBand _band = new DevExpress.XtraGrid.Views.BandedGrid.GridBand();
                col.VisibleIndex = i++;
                switch (col.FieldName)
                {
                    case "rowindex":
                        col.Width = 55;
                        _band.Caption = "No";
                        break;
                    case "ProductNo":
                        col.Width = 200;
                        _band.Caption = "Product No";
                        break;
                    case "ProudctName":
                        col.Width = 350;
                        _band.Caption = "Product Name";
                        break;
                    case "QuantityReceiving":
                        col.Width = 120;
                        _band.Caption = "Receiving Quantity";
                        break;
                    case "QuantityDelivering":
                        col.Width = 120;
                        _band.Caption = "Delivering Quantity";
                        break;
                }
                _band.Columns.Add(col);
                bandedGridView1.Bands.Add(_band);
            }
            FormatXtraGridView(gridControl);
            DinhDangGridBandVaColumns(bandedGridView1);
            bandedGridView1.OptionsPrint.AutoWidth = true;
            bandedGridView1.BandPanelRowHeight = 40;
            FormatNumberInGridView(bandedGridView1);
            #endregion

        }
        protected override void PreviewReport()
        {
            try
            {
                string time = "From " + dtNgayBatDau.DateTime.ToString("dd/MM/yyyy") + " to " + dtNgayKetThuc.DateTime.ToString("dd/MM/yyyy");
                PrintWithHeaderAndFooter(false, gridControl, true, true, "\nTOTAL DELIVERING AND RECEIVING REPORT", time, "", "Người lập\n(Ký, họ tên)",
                        "Kế toán trưởng\n(Ký, họ tên)", "Thủ trưởng đơn vị\n(Ký, họ tên, đóng dấu)");
            }
            catch { }
        }
    }
}
